﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LiveFX.Client;
using Microsoft.LiveFX.ResourceModel;
using System.Windows.Forms;
using System.IO;

namespace LiveFrameworkExplorer.Plugins.FileBrowser
{
    public class FileUploadingPlugin : IPlugin, IPluginItem
    {
        private ILFEBase _lfeBase;

        #region IPlugin Members

        public void Init(ILFEBase lfeBase)
        {
            _lfeBase = lfeBase;
        }

        public List<Type> GetTypes()
        {
            return new List<Type>() { 
                typeof(DataFeedResource), typeof(DataFeed)
            };
        }

        public string GetMenuItemLabel()
        {
            return "Upload a file";
        }

        public System.Drawing.Image GetMenuItemImage()
        {
            return LiveFrameworkExplorer.Properties.Resources.upload;
        }

        public bool CheckItem<TMeshItem, TResource>(object item)
            where TMeshItem : Microsoft.LiveFX.Client.LiveItem<TResource>, new()
            where TResource : Microsoft.LiveFX.ResourceModel.Resource, new()
        {
            var feed = item as DataFeed;
            if (feed != null)
                return feed.Resource.Type == "LiveMeshFiles";
            else
                return false;
        }

        public void OpenPlugin<TMeshItem, TResource>(object item, object parentItem)
            where TMeshItem : Microsoft.LiveFX.Client.LiveItem<TResource>, new()
            where TResource : Microsoft.LiveFX.ResourceModel.Resource, new()
        {
            var feed = item as DataFeed;
            OpenFileDialog ofp = new OpenFileDialog();
            ofp.Title = "Upload file to your Mesh";
            if (ofp.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileInfo = new FileInfo(ofp.FileName);
                _lfeBase.GetDTEEnvironment().StatusBar.Progress(true, "Starting upload of " + ofp.FileName, 1, 5);
                FileStream fileStream = File.Open(ofp.FileName, FileMode.Open);
                feed.DataEntries.Add(fileStream, fileInfo.Name, fileInfo.GetMimeType());
                feed.UpdateCompleted += (object sender, System.ComponentModel.AsyncCompletedEventArgs e) =>
                {
                    if (e.Error == null)
                    {
                        _lfeBase.GetDTEEnvironment().StatusBar.Progress(false, "Upload finnished !", 5, 5);
                        MessageBox.Show(e.UserState as String + " was successfull uploaded !", "Live Framework Explorer",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //_lfeBase.ReloadLFE();
                        _lfeBase.Refresh<TMeshItem, TResource>();
                    }
                    else
                    {
                        _lfeBase.GetDTEEnvironment().StatusBar.Text = "Error to upload " + e.UserState as String;
                        _lfeBase.WriteToLFEOutput("Error to upload " + e.UserState as String + " : " + e.Error.Message);
                    }
                };
                feed.UpdateAsync(fileInfo.Name);
            }
        }

        #endregion
    }
}
